package com.guo.anim.sunset;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.guo.anim.R;

/**
 * 自定义视图实现星星
 */
public class StarView extends View {
    /**
     * 绘制星星所需的内径和外径
     */
    private int outRadius , innerRadius;
    private Paint mPaint;
    private int[][] points = new int[10][2];
    private static final int NUM_POINTS = 10;
    /**
     * 视图的宽度和高度
     */
    private int width,height;
    /**
     * 星星在父类视图的左上坐标，半径
     */
    private int x , y;
    private int size;
    public StarView(Context context) {
        super(context);
        initPaint();
    }

    public StarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public StarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint(){
        if(null == mPaint){
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(3);
            mPaint.setColor(getResources().getColor(R.color.purple_200));
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
        height = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
        outRadius = Math.min(width,height)/4;
        innerRadius = outRadius/2;
        getPositions();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawStar(canvas);
    }

    /**
     * 使用Path绘制星星
     * @param canvas
     */
    private void drawStar(Canvas canvas){
        Path path = new Path();
        for(int i = 0 ; i < NUM_POINTS ; i++){
            if(i == 0){
                path.moveTo(points[i][0] , points[i][1]);
            }else{
                path.lineTo(points[i][0] , points[i][1]);
            }
        }
        path.close();
        canvas.drawPath(path,mPaint);
    }

    /**
     * 计算星星的顶点坐标
     * @return
     */
    private int[][] getPositions(){
        int center_x = width/2 + getPaddingLeft();
        int center_y = height/2 + getPaddingTop();
        for(int i = 0 ; i < NUM_POINTS ; i++){
            int r = (i%2 == 0 ? outRadius : innerRadius);
            double angle = -Math.PI / 10 + i*2*Math.PI / 10 ;
            //横坐标
            points[i][0] = (int)(r*Math.cos(angle)) + center_x;
            //纵坐标
            points[i][1] = (int)(r*Math.sin(angle)) + center_y;
        }
        return points;
    }

    public int getPositionX() {
        return x;
    }

    public void setPositionX(int x) {
        this.x = x;
    }

    public int getPositionY() {
        return y;
    }

    public void setPositionY(int y) {
        this.y = y;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
}
